<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item :label="$t('时间')">
              <a-date-picker
                :show-time="{ defaultValue: moment('00:00:00', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择开始时间')"
                class="query-group-cust"
                v-model="queryParam.bTime"
              ></a-date-picker>
              <span class="query-group-split-cust"></span>
              <a-date-picker
                :show-time="{ defaultValue: moment('23:59:59', 'HH:mm:ss') }"
                valueFormat="YYYY-MM-DD HH:mm:ss"
                :placeholder="$t('请选择结束时间')"
                class="query-group-cust"
                v-model="queryParam.eTime"
              ></a-date-picker>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('设备类型')">
              <a-select v-model="queryParam.deviceType">
                <a-select-option :value="item.type" :key="index" v-for="(item, index) in devicetypeList"
                  >{{item.type+'('+item.type_dictText+')'}}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item :label="$t('设备')">
              <a-select show-search :filter-option="false" @search="handleSearch" v-model="queryParam.deviceId">
                <a-select-option :value="item.id" :key="index" v-for="(item, index) in deviceList">
                  <span>{{ item.name }}</span>
                  <span v-if="item.name != $t('全部')">({{ item.id }})</span>
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :xl="8" :lg="12" :md="12" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('查询') }}</a-button>
              <a-dropdown>
                <a-menu slot="overlay" @click="handleDateClick">
                  <a-menu-item key="1"> {{$t('今日')}} </a-menu-item>
                  <a-menu-item key="2"> {{$t('近三天')}} </a-menu-item>
                  <a-menu-item key="3"> {{$t('近七天')}} </a-menu-item>
                  <a-menu-item key="4"> {{$t('近一月')}} </a-menu-item>
                  <a-menu-item key="5"> {{$t('近三月')}} </a-menu-item>
                  <a-menu-item key="6"> {{$t('近半年')}} </a-menu-item>
                  <a-menu-item key="7"> {{$t('本年')}} </a-menu-item>
                </a-menu>
                <a-button type="primary" style="margin-left: 8px"> {{ $t('快捷查询') }} <a-icon type="down" /></a-button>
              </a-dropdown>
              <a-button type="primary" style="margin-left: 8px" @click="searchReset">{{
                $t('重置')
              }}</a-button>
              <a-button type="primary" :loading="exloading" style="margin-left: 8px" @click="handleExportXls($t('存币存票汇总'))" v-has="'MemberDepositSummaryController:export'">{{
                $t('导出')
              }}</a-button>
            </span>
          </a-col>
          <template v-if="toggleSearchStatus"></template>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->
    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="deviceId"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="false"
        :loading="loading"
        :customHeaderRow="customHeaderRow"
        :customRow="customRow"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无图片') }}</span>
          <img
            v-else
            :src="getImgView(text)"
            height="25px"
            alt
            style="max-width: 80px; font-size: 12px; font-style: italic"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px; font-style: italic">{{ $t('无文件') }}</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">{{
            $t('下载')
          }}</a-button>
        </template>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import moment from 'moment'
import '@/assets/less/TableExpand.less'
import { getAction } from '@/api/manage'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'

export default {
  name: 'DepositCoinLotterySummary',
  mixins: [JeecgListMixin],
  components: {},
  data() {
    return {
      moment,
      description: '存币存票汇总页面',
      // 表头
      columns: [
        {
          title: this.$t('设备编码'),
          align: 'center',
          dataIndex: 'deviceId',
          customRender:(t,r)=>{
            if(t=='合计'){
              return this.$t(t)
            }
            return t
          }
        },
        {
          title: this.$t('设备名称'),
          align: 'center',
          dataIndex: 'deviceName',
        },
        {
          title: this.$t('设备类型'),
          align: 'center',
          dataIndex: 'deviceType_dictText',
        },
        {
          title: this.$t('存币次数'),
          align: 'center',
          dataIndex: 'coinCount',
          customCell: (r, i) => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('存币值'),
          align: 'center',
          dataIndex: 'coin',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(2);
          }
        },
        {
          title: this.$t('存票次数'),
          align: 'center',
          dataIndex: 'lotteryCount',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
        {
          title: this.$t('存票值'),
          align: 'center',
          dataIndex: 'lottery',
          customCell: () => {
            return {
              style: {
                textAlign: 'right',
              },
            }
          },
          customRender: (text, r) => {
            text = (text ==null || text =="" || text ==undefined)?0:parseFloat(text);
            return text.toFixed(0);
          }
        },
      ],
      dataSource: [],
      queryParam: {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        deviceId: '',
      },
      url: {
        list: '/report/day/memberDepositSummary/list',
        exportXlsUrl: '/report/day/memberDepositSummary/export',
      },
      devicetypeList: [],
      deviceList: [],
    }
  },
  mounted() {},
  methods: {
    customRow(r, i) {
      return {
        on:{
          click: () => {
            let x = this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i})
            if( x == -1 ){
              this.selectedRowIndex.push('a'+this.ipagination.current+''+i)
            }else{
              this.selectedRowIndex.splice(x, 1)
            }
          }
        },
        style: {
          whiteSpace: 'break-spaces',
          wordBreak: 'break-word',
          overflowWrap: 'break-word',
          backgroundColor: this.selectedRowIndex.findIndex(r=>{return r=='a'+this.ipagination.current+''+i}) > -1 ? '#FFECE6' : '',
          fontWeight: r.deviceId == '合计' ? 700 : 400,
        },
      }
    },
    initDictConfig() {
      this.handleSearch('')
      getAction('device/bisDeviceInfo/getTypeList', {}).then((res) => {
        if (res.success) {
          this.devicetypeList = res.result.records
        }
      })
      this.searchQuery()
    },
    handleSearch(value) {
      let query = {}
      if (value) {
        query.deviceInfo = value
      }
      getAction('/device/bisDeviceInfo/list', query).then((res) => {
        if (res.success) {
          this.deviceList = res.result.records
          this.deviceList.unshift({ id: '', name: this.$t('全部') })
        }
      })
    },
    searchReset() {
      this.queryParam = {
        bTime: moment().format('YYYY-MM-DD 00:00:00'),
        eTime: moment().format('YYYY-MM-DD 23:59:59'),
        deviceId: '',
      }
      this.searchQuery()
    },
    handleDateClick(evt){
      if(evt.key=='1'){
        this.queryParam.bTime=moment().format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='2'){
        this.queryParam.bTime=moment().subtract(2,'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='3'){
        this.queryParam.bTime=moment().subtract(6,'day').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='4'){
        this.queryParam.bTime=moment().subtract(1,'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='5'){
        this.queryParam.bTime=moment().subtract(3,'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='6'){
        this.queryParam.bTime=moment().subtract(6,'month').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      if(evt.key=='7'){
        this.queryParam.bTime=moment().startOf('year').format('YYYY-MM-DD 00:00:00')
        this.queryParam.eTime=moment().format('YYYY-MM-DD 23:59:59')
      }
      this.searchQuery()
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>